package informes;

import Utilidades.*;
import controlador.RegistroParkingControlador;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import modelo.RegistroParkingModelo;
import modelo.entidades.RegistroParking;
import vista.*;
import vista.RegistroParking.RegistroParkingVista;

/**
 *
 * @author ajb
 */
public class InformeRegistroParkingVistaImp extends AbstractVistaImp<RegistroParkingControlador, RegistroParkingModelo> implements RegistroParkingVista {

    private RegistroParkingControlador rac;
    private RegistroParkingModelo ram;

    public InformeRegistroParkingVistaImp(RegistroParkingControlador rac, RegistroParkingModelo ram) {
        this.rac = rac;
        this.ram = ram;

        initComponents();
    }

    @Override
    public void refresh() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jBinforme = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(395, 245));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/Img/casco4.png"))); // NOI18N

        jBinforme.setText("Informes");
        jBinforme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBinformeActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre del Parking");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                        .addGap(135, 135, 135))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(jBinforme)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jBinforme))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

     private void jBinformeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBinformeActionPerformed
            generarInforme();
         
     }//GEN-LAST:event_jBinformeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBinforme;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    private void generarInforme() {
        List<RegistroParking> registros = new ArrayList<RegistroParking>();

        Date fechaactual = new Date();
        SimpleDateFormat formato = new SimpleDateFormat("yyyMMdd_H");
        String cadenaFecha = formato.format(fechaactual);

        String ruta = Configuracion.getPropiedad("Ruta");      
        
        File nruta = new File(ruta);
        if (!nruta.exists()) {
            Errores.mostrarInformacion("La ruta  " + nruta + " debe estar creada previamente");
        } else {
            String sFichero = nruta + "\\Reporte" + cadenaFecha + ".txt";
            try {
                registros = ram.listar();
            } catch (SQLException ex) {
                Errores.mostrarError(ex);
            } catch (Exception ex) {
                Errores.mostrarError(ex);
            }

            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter(sFichero));
            } catch (IOException ex) {
                Errores.mostrarError(ex);
            }
            try {
                bw.write("Nombre del Parking \t" + "Matricula \t " + "Fecha Entrada \t " + "Fecha Salida \t " + "\n");
            } catch (IOException ex) {
                Errores.mostrarError(ex);
            }
            for (RegistroParking reg : registros) {
                try {
                    if (reg.getParking().getNombre().equalsIgnoreCase(this.jTextField1.getText())) {
                        bw.write(reg.getParking().getNombre() + "\t" + reg.getVehiculo().getMatricula() + "\t" + reg.getFechaEntrada() + "\t" + reg.getFechaSalida() + "\n");
                    }
                } catch (IOException ex) {
                    Errores.mostrarError(ex);
                }
            }
            try {
                // Hay que cerrar el fichero
                bw.close();
            } catch (IOException ex) {
                Errores.mostrarError(ex);
            }
            Errores.mostrarInformacion("Fichero con nombre :" + sFichero + "\n Creado Exitosamente en la ruta establecida");
        }
    }
}
